/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Feb 15, 2012  10:34:29 AM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : apps.promis.in.common
 * File Name : CostArticleSubGroupAction.java
 * TODO
 */
package apps.promis.in.common;

import java.util.List;

import jpa.entitys.promis.common.CostArticleSubGroup;
import jpa.entitys.promis.common.CostArticleSubGroupPK;
import jpa.entitys.synchro.staging.MmMatmasInb;
import jpa.service.promis.common.CostArticleSubGroupServices;
import jpa.service.synchro.staging.MmMatmasInbServices;

import org.apache.log4j.Logger;

import promis.utility.ACTION_HANDLER;
import promis.utility.CommonService;
import promis.utility.Utilitys;
import apps.sessionFactory;

/**
 * 
 * @author Chatree Siangklom of Administrator
 * Date =Nov 13, 2013 : 2:34:17 PM 
 * CreateBy=Chatree Siangklom
 * 
 * CostArticleSubGroupAction
 * TODO
 */
public class CostArticleSubGroupAction {
	
	private static final Logger log = Logger.getLogger(CostArticleSubGroupAction.class);
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:34:21 PM 
	 * CreateBy=Chatree Siangklom
	 * CostArticleSubGroupAction
	 * TODO
	 */
	public CostArticleSubGroupAction() {
		// TODO Auto-generated constructor stub		
		
	}
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:34:24 PM 
	 * CreateBy=Chatree Siangklom
	 * CostArticleSubGroup
	 * TODO
	 * @param articleGroup
	 * @param articleSubGroup
	 * @return
	 * @throws Exception
	 */
	public CostArticleSubGroup FindByKey(String articleGroup, String articleSubGroup) throws Exception {
		
		CostArticleSubGroupServices costArticleSubGroupSrv = new CostArticleSubGroupServices(sessionFactory.CreateEmPromis());
		CostArticleSubGroupPK id = new CostArticleSubGroupPK();
		id.setArticleGroup(CommonService.IsNullRNull(articleGroup));
		id.setArticleSubGroup(CommonService.IsNullRNull(articleSubGroup));
		
		return costArticleSubGroupSrv.findById(id);
	}
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:35:12 PM 
	 * CreateBy=Chatree Siangklom
	 * boolean
	 * TODO
	 * @param articleGroup
	 * @param articleSubGroup
	 * @return
	 * @throws Exception
	 */
	public boolean ExistingChecked(String articleGroup, String articleSubGroup) throws Exception {
		return FindByKey(articleGroup, articleSubGroup) != null ? true : false; 
	}
	
	public CostArticleSubGroup SaveOrUpdateArticleSubGroup(MmMatmasInb mmMatmasInb) throws Exception {
		
		try{
			
			CostArticleSubGroupServices costArticleSubGroupSrv = new CostArticleSubGroupServices(sessionFactory.CreateEmPromis());
			CostArticleSubGroup model = FindByKey(mmMatmasInb.getArtiGrpCode(), mmMatmasInb.getArtiSubGrpCode());
			
			if(model == null)
			{
				//Insert
				model = new CostArticleSubGroup();

				//Set Id 
				CostArticleSubGroupPK id = new CostArticleSubGroupPK();
				id.setArticleGroup(CommonService.IsNullRNull(mmMatmasInb.getArtiGrpCode()));
				id.setArticleSubGroup(CommonService.IsNullRNull(mmMatmasInb.getArtiSubGrpCode()));
				
				model.setId(id);
				model.setArticleType(mmMatmasInb.getArtiTypeCode());
				model.setDescription(mmMatmasInb.getArtiSubGrpName());
				model.setSyncIssync("N");
				
				model.setCreatedOn(Utilitys.ConvertStringToDateTime(mmMatmasInb.getCreatedOn()));
				model.setCreatedBy(mmMatmasInb.getCreatedBy());
				
				return costArticleSubGroupSrv.InsertedOrUpdated(model, ACTION_HANDLER.INSERTED);
				
			}else{
				
				//Update
				model.setArticleType(mmMatmasInb.getArtiTypeCode());
				model.setDescription(mmMatmasInb.getArtiSubGrpName());
				model.setSyncIssync("N");
				
				if(CommonService.IsNull(mmMatmasInb.getUpdatedOn()))
					model.setLastUpdOn(Utilitys.ConvertStringToDateTime(mmMatmasInb.getCreatedOn()));				
				else
					model.setLastUpdOn(Utilitys.ConvertStringToDateTime(mmMatmasInb.getUpdatedOn()));
								
				model.setLastUpdBy(CommonService.IsNullRNull(mmMatmasInb.getCreatedBy()));
				
				return costArticleSubGroupSrv.InsertedOrUpdated(model, ACTION_HANDLER.UPDATED);
			}
			
		}catch(Exception e){
			System.out.println(e.getMessage());
			log.error(e.getMessage());	
			throw e;
		}	
	}
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:35:18 PM 
	 * CreateBy=Chatree Siangklom
	 * void
	 * TODO
	 * @throws Exception
	 */
	private void testedd () throws Exception {
		
		MmMatmasInbServices obj = new MmMatmasInbServices(sessionFactory.CreateEmSynchro());
		List<MmMatmasInb> lst =  obj.findAllIsSync("N");
		log.info("lst.size : " +lst.size());
		
		for(MmMatmasInb mmMatmasInb : lst){
			
			log.info("ExistingChecked : " + ExistingChecked(mmMatmasInb.getArtiGrpCode(), mmMatmasInb.getArtiSubGrpCode()));
			log.info("mmMatmasInb.getArtiGrpCode : " + mmMatmasInb.getArtiGrpCode());
			log.info("mmMatmasInb.getArtiSubGrpCode : " + mmMatmasInb.getArtiSubGrpCode());
			
			SaveOrUpdateArticleSubGroup(mmMatmasInb);
		}
		
	}
	
//	public static void main(String [] args){
//		CostArticleSubGroupAction xx = new  CostArticleSubGroupAction();
//		try {
//			
//			xx.testedd();
//			
//			CostArticleSubGroup obj = xx.FindByKey("715","001");
//			
//			log.info(xx.ExistingChecked("715","001"));
//			log.info(xx.ExistingChecked("715","002"));
//			
//			log.info(obj.getDescription());
//			log.info(obj.getArticleType());
//			
//			
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}

}
